//==================================================
// D_NC_OW_electricmeter_temperature.ino
// 2015-02-06
//==================================================
// Nabton Client Generic Configuration
//==================================================

#define NFLOAT 2  // No of decimals i float value
#define SIDN  2   // No of SIDs
#define SID1 5
#define SID2 6
#define SID3 63
#define SID4 904
#define SID5 905
#define SID6 906
#define SID7 907
#define SID8 908

#define MAX_SID 10
#define MAX_ORDERS 100
char g_msg[120];
int g_sids[10] = {SIDN,SID1,SID2,SID3,SID4,SID5,SID6,SID7,SID8};
#define PIN_LED_STATUS 13

// Arduino-RPi protocol
#define NABTON_DATA     1 

//=================================================
//
// D0 RX used for serial communication to server (Raspberry Pi)
// D1 TX used for serial communication to server (Raspberry Pi)
// D2 
// D3 Message Pin (blink led)
// D4 
// D5 
// D6 
// D7 Led Pulse
// D8 
// D9 One Wire Data
// D10   
// D11  
// D12 
// D13
//
// A0
// A1
// A2
// A3
// A4 
// A5 
//=================================================
//==================================================
// Nabton Client Application Configuration
//==================================================
#include <OneWire.h>
#include <DallasTemperature.h>
#define PIN_LED_PULSE 7
unsigned long ttemp;
unsigned long time1, time2, data;
int count = 0;
//=================================================
// One Wire
//=================================================
#define ONE_WIRE_BUS 9
#define TEMPERATURE_PRECISION 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress device[MAX_SID];
int nsensors = 0;
//=================================================
void NB_printFloat(float value, int places) 
//=================================================
{
  int digit;
  float tens = 0.1;
  int tenscount = 0;
  int i;
  float tempfloat = value;
  float d = 0.5;
  
  if (value < 0) d *= -1.0;
  for (i = 0; i < places; i++) d/= 10.0;    
  tempfloat +=  d;
  if (value < 0) tempfloat *= -1.0;
  while ((tens * 10.0) <= tempfloat) 
  {
    tens *= 10.0;
    tenscount += 1;
  }
  if (value < 0) Serial.print('-');
  if (tenscount == 0) Serial.print(0, DEC);
  for (i=0; i< tenscount; i++) 
  {
    digit = (int) (tempfloat/tens);
    Serial.print(digit, DEC);
    tempfloat = tempfloat - ((float)digit * tens);
    tens /= 10.0;
  }
  if (places <= 0) return;
  Serial.print('.');  
  for (i = 0; i < places; i++) 
  {
    tempfloat *= 10.0; 
    digit = (int) tempfloat;
    Serial.print(digit,DEC);  
    tempfloat = tempfloat - (float) digit; 
  }
}
//=================================================
void NB_sendFloatData(int sid, float value)
//=================================================
{
    digitalWrite(PIN_LED_STATUS,HIGH);
    Serial.print(NABTON_DATA);Serial.print(" "); 
    Serial.print(sid);Serial.print(" "); 
    Serial.print(NFLOAT);Serial.print(" "); 
    NB_printFloat(value,NFLOAT);
    Serial.print(";");
    digitalWrite(PIN_LED_STATUS,LOW);
    //strcpy(dl[3],p);
} 
//======================================
void pulse()
//======================================
{ 
      time2 = time1;
      time1 = millis();
      data = time1 - time2;
      float watt = 3600.0/data*1000.0;
      digitalWrite(PIN_LED_PULSE,HIGH);
      delay(2);
      digitalWrite(PIN_LED_PULSE,LOW);
      NB_sendFloatData(SID1, watt);
}
//======================================
void setup()
//======================================
{
  Serial.begin(9600);
  pinMode(PIN_LED_PULSE,OUTPUT);
  pinMode(PIN_LED_STATUS,OUTPUT);
  digitalWrite(PIN_LED_STATUS,HIGH);
  
  // One Wire
  sensors.begin();
  nsensors = sensors.getDeviceCount();
  if(nsensors > 0)
  {
    for(int i=0;i<nsensors;i++)
    {
      sensors.getAddress(device[i], i);
      sensors.setResolution(device[i], TEMPERATURE_PRECISION);
    }
  }
  
  
  attachInterrupt(0, pulse, FALLING);

}

//======================================
void loop()
//======================================
{
    float tempC;
    digitalWrite(PIN_LED_STATUS,HIGH);
    delay(500);
    if(count == 15)
    {
      sensors.requestTemperatures();
      tempC = sensors.getTempC(device[0]);    
      NB_sendFloatData(SID2,tempC);    
      count = 0;
    }
    digitalWrite(PIN_LED_STATUS,LOW);
    delay(500);
    count++;
}
